//
//  TCMainViewController.m
//  InterviewDemo
//
//  Created by TonyChan on 16/6/9.
//  Copyright © 2016年 TonyChan. All rights reserved.
//

#import "TCMainViewController.h"
#import "TCWebView.h"
#import "TCCameraImage.h"
#import "TCMacro.h"
#import "TCCameraViewController.h"

@interface TCMainViewController ()< UIWebViewDelegate>

@property (nonatomic, strong) TCWebView *webView;
@property (nonatomic, strong) JSContext *jsContext;
@property (nonatomic, strong) TCCameraImage *cameraImage;

@end

@implementation TCMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self.view addSubview:self.webView];
    
    NSString *webFilePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"];
    NSURL *webURL = [NSURL URLWithString:webFilePath];
    NSURLRequest *webRequest = [NSURLRequest requestWithURL:webURL];
    [self.webView loadRequest:webRequest];
}

#pragma mark - web view delegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self jsInitialize];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error {
    
}

#pragma mark - action

- (void)jsInitialize {
    self.jsContext = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    @weakify(self)
    self.jsContext[@"openCamera"] = ^(NSString *height, NSString *wide, NSString *camera, NSString *option) {
        @strongify(self)
        self.cameraImage.imageHeight = height;
        self.cameraImage.imageWide = wide;
        
        NSUInteger cameraTypeIndex = [self.cameraImage.cameraTypeArray indexOfObject:camera];
        if (cameraTypeIndex == NSNotFound) {
            NSLog(@"not found!");
        } else{
            self.cameraImage.cameraType = cameraTypeIndex+1;
        }
        
        NSUInteger sourceTypeIndex = [self.cameraImage.sourceTypeArray indexOfObject:option];
        if (sourceTypeIndex == NSNotFound) {
            NSLog(@"not found!");
        } else{
            self.cameraImage.sourceType = sourceTypeIndex+1;
        }
        [self loadCamera:self.cameraImage];
    };
}

- (void)loadCamera:(TCCameraImage *)cameraImage {
    NSString *identifier = @"";
    switch (cameraImage.sourceType) {
        case TCImageSourceCamera:
            identifier = @"PushToCamera";
            break;
        case TCImageSourceAlbum:
            identifier = @"PushToProcessImage";
            break;
        case TCImageSourceAll:
            break;
        default:
            break;
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [self performSegueWithIdentifier:identifier sender:nil];
    });
}

#pragma mark - getter

- (JSContext *)jsContext {
    if (!_jsContext) {
        _jsContext = [[JSContext alloc] init];
    }
    return _jsContext;
}

- (TCWebView *)webView {
    if (!_webView) {
        _webView = [[TCWebView alloc] initWithFrame:self.view.bounds];
        _webView.delegate = self;
    }
    return _webView;
}

- (TCCameraImage *)cameraImage {
    if (!_cameraImage) {
        _cameraImage = [[TCCameraImage alloc] init];
    }
    return _cameraImage;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

@end
